Skip to content

Improve Sys.time error handling#115

Draft
MisterDA wants to merge 4 commits intotrunkfrom
win32-clock-sys-time
Draft

Improve Sys.time error handling#115
MisterDA wants to merge 4 commits intotrunkfrom
win32-clock-sys-time

Conversation

@MisterDA
Copy link
Owner

@MisterDA MisterDA commented Jan 17, 2025

  • Fix caml_win32_clock overflowing if wall-clock exceeds 24.8 days

    From the docs:
    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/clock?view=msvc-170

    Given enough time, the value returned by clock can exceed the maximum positive value of clock_t. When the process has run longer, the value returned by clock is always (clock_t)(-1), as specified by the ISO C99 standard (7.23.2.1) and ISO C11 standard (7.27.2.1). Microsoft implements clock_t as a long, a signed 32-bit integer, and the CLOCKS_PER_SEC macro is defined as 1000. This macro gives a maximum clock function return value of 2147483.647 seconds, or about 24.8 days. Don't rely on the value returned by clock in processes that have run for longer than this amount of time.

  • Make Sys.time return -1 on error instead of undefined value

@MisterDA MisterDA force-pushed the win32-clock-sys-time branch 2 times, most recently from 7fb9d63 to 343262c Compare January 21, 2025 11:57
@MisterDA MisterDA changed the title Win32 clock sys time Improve Sys.time error handling Jan 23, 2025
From the docs:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/clock?view=msvc-170

> Given enough time, the value returned by `clock` can exceed the
> maximum positive value of `clock_t`. When the process has run
> longer, the value returned by `clock` is always `(clock_t)(-1)`, as
> specified by the ISO C99 standard (7.23.2.1) and ISO C11
> standard (7.27.2.1). Microsoft implements `clock_t` as a `long`, a
> signed 32-bit integer, and the `CLOCKS_PER_SEC` macro is defined as
> 1000. This macro gives a maximum `clock` function return value of
> 2147483.647 seconds, or about 24.8 days. Don't rely on the value
> returned by `clock` in processes that have run for longer than this
> amount of time.
Sys.times now returns -1 if POSIX clock or times fail on
overflow. Unix.times now raises on Unix (it would raise only on
Windows).

times: https://pubs.opengroup.org/onlinepubs/9799919799/functions/times.html
clock: https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock.html
@MisterDA MisterDA force-pushed the win32-clock-sys-time branch from 343262c to 264b728 Compare January 24, 2025 16:51
caml_win32_clock can only run for 24.8 days before overflowing. If we
use a uint64_t accounting for 100-nsec intervals, and convert to
double (where the maximum integer value is 2**53), the function will
report accurate running times for over 28 years.
@MisterDA MisterDA force-pushed the win32-clock-sys-time branch from 264b728 to 5d3a425 Compare January 25, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant